package com.tedu.eguser1.controller;

import com.tedu.eguser1.base.response.JsonResult;
import com.tedu.eguser1.mapper.UserMapper;
import com.tedu.eguser1.pojo.dto.UserAddParam;
import com.tedu.eguser1.pojo.dto.UserUpdateParam;
import com.tedu.eguser1.pojo.entity.User;
import com.tedu.eguser1.pojo.vo.UserListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
@Slf4j
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/add")
    public JsonResult insert(UserAddParam userParam){
        User user = new User();
        BeanUtils.copyProperties(userParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int num = userMapper.insert(user);
        return JsonResult.success();
    }
    @GetMapping("/list")
    public JsonResult select(){
        log.debug("开始查询" );
        List<UserListVO> list = userMapper.select();
        return JsonResult.success(list);
    }
    @PostMapping("/del")
    public JsonResult delete(Integer id){
        int num = userMapper.delete(id);
        return JsonResult.success();
    }
    @PostMapping("/update")
    public JsonResult update(UserUpdateParam userUpdateParam){
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int num = userMapper.update(user);
        return JsonResult.success();
    }
}
